import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Shapes 1.15

ParaCalSubItem {
    id: root
    title: qsTr("通道配置")
    width: 784
    height: 338
    property var addrs:mytables.addrs
    function getAddrs() {
            var str="";
            for (var i = 0; i < grid.children.length; i++) {
                if(grid.children[i].addrIndex !== undefined){
                    str+=grid.children[i].addrIndex+"|";
                }
            }
            return str;
        }
    onSignalPre: {
    }
    onSignalNext: {
    }
    onSignalOK: {
        var result = getAddrs();
        if(mymodbus.setAddrs(result)){
            paraCalView.signalSuccess()
        }
        else{
            paraCalView.signalFailed()
        }
    }
Component.onCompleted: {
    var lst = mymodbus.addrs;
    var arr = lst.split("|");
    //var addrs = []

    for(let i= 0;i<arr.length;i++){
        if(arr[i].length>0){
            addrs.push(Number(arr[i]))
        }
    }
    var count=Math.min(addrs.length,grid.children.length)
    for (let j = 0; j < count; j++) {
        //grid.children[j].addrIndex=addrs[j];
        if (grid.contentItem && grid.contentItem.children && grid.contentItem.children[j]) {
            grid.contentItem.children[j].addrIndex = addrs[j];
        }
    }
}

ScrollView
{
    y:94
    anchors.centerIn: parent
    width: 160*mymodbus.axisCount>784?784:160*mymodbus.axisCount
    height: 224
    Grid{  // Row、Column、Flow
            width:160*mymodbus.axisCount
            rows:1
            id:grid
            anchors.margins: 8;
            spacing: 4; // 间隔
            height: 224
            Repeater{
                model: mymodbus.axisCount;  // 子项目个数
                // Rectangle{
                //     width: 200;
                //     height: 200;
                //     property int colorIndex: Math.floor(Math.random()*3);　　// Math.floor(Math.random()*3)生成0~2的随机数
                //     color: root.colorArray[colorIndex];
                //     border.color: Qt.lighter(color);
                //     Text {
                //         anchors.centerIn: parent;
                //         color: "#f0f0f0";
                //         text: "Cell " + index
                //     }
                // }
                delegate:  Card{
                    no:index
                    addrIndex: index < addrs.length ? addrs[index] : -1
                }
            }
        }
}

}
